// axios的二次封装 哪里需要发送ajax哪里调用该函数
// 公共路径 baseURL
import axios  from "axios";
import qs from 'qs';

// axios.defaults.属性  eg:axios.defaults.baseURL
axios.defaults.baseURL = "http://localhost:8001";
axios.defaults.timeout = 5000;

function requestGet(url,params){
    return new Promise((resolve,reject)=>{
        axios({
            method:'get',
            url,
            headers:{},
            transformRequest:[],
            transformResponse:[
                function(data){
                    return JSON.parse(data);
                }
            ],
            params,
        }).then((res)=>{
            console.log(res);
            resolve(res);
        }).catch(err=>{
            console.log(err);
            reject(err);
        })
    })
   
}
function requestPost(url,data){
    return  new Promise((resolve,reject)=>{
        axios({
            method:'post',
            url,
            headers:{
                "Content-Type":"application/x-www-form-urlencoded;chartset=utf-8"
            },
            transformRequest:[function(data){
                // 序列化
                return qs.stringify(data);
            }],
            transformResponse:[],
            data
        }).then(res=>{
            resolve(res);
        }).catch(err=>{
            reject(err);
        })
    })
}
export {
    requestGet,
    requestPost
}